package com.gome.ocean.service.task;

import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import com.alibaba.druid.pool.DruidDataSource;

/**
 * 数据源
 * 
 * @author haqiaolong 2016年3月30日 下午9:05:15
 * @since 5.0.0
 */
public class DataSourceWrapper {

    private JdbcTemplate    jdbcTemplate;
    private DruidDataSource druidDataSource;

    public DataSourceWrapper(DruidDataSource druidDataSource){
        this.druidDataSource = druidDataSource;
        this.jdbcTemplate = new JdbcTemplate(druidDataSource);
    }

    public TableCheck queryForTableCheck(final String sql) {
        return jdbcTemplate.queryForObject(sql, new TableCheck());
    }

    public int update(final String sql) {
        return jdbcTemplate.update(sql);
    }

    public int update(final String sql, Object... args) {
        return jdbcTemplate.update(sql, args);
    }

    public String getDbType() {
        return druidDataSource.getDbType();
    }

    public DruidDataSource getDruidDataSource() {
        return druidDataSource;
    }

    public void close() {
        druidDataSource.close();
        jdbcTemplate = null;
    }

    class TableCheck implements RowMapper<TableCheck>, Serializable {

        /**
         * 
         */
        private static final long serialVersionUID = 4635839794662787842L;

        private Long              rowSize;
        private Long              indexSize;

        public Long getRowSize() {
            return rowSize;
        }

        public void setRowSize(Long rowSize) {
            this.rowSize = rowSize;
        }

        public Long getIndexSize() {
            return indexSize;
        }

        public void setIndexSize(Long indexSize) {
            this.indexSize = indexSize;
        }

        @Override
        public TableCheck mapRow(ResultSet rs, int rowNum) throws SQLException {
            TableCheck tableCheck = new TableCheck();
            tableCheck.setRowSize(rs.getLong(1));
            tableCheck.setIndexSize(rs.getLong(2));
            return tableCheck;
        }
    }

    @Override
    public String toString() {
        return "DataSourceWrapper [druidDataSource=" + druidDataSource + "]";
    }

}
